package com.ewanghuiju.app.presenter.resale;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.resale.ResaleSureOrderContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.exception.ApiException;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 转售确认订单
 * @time 2020/2/16
 */
public class ResaleSureOrderPresenter extends RxPresenter<ResaleSureOrderContract.View> implements ResaleSureOrderContract.Presenter {

    private DataManager mDataManager;
    private int currentType = Constants.TYPE_ADDRESS_SELECTED;

    @Inject
    public ResaleSureOrderPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    /**
     * 转售购买订单
     *
     * @param goods_id
     * @param goods_num
     * @param address_id
     * @param pay_password
     */
    @Override
    public void nowPay(String goods_id, String goods_num, String address_id, String pay_password, String orderRemarks) {
        addSubscribe(mDataManager.integralConversion(goods_id, goods_num,
                address_id, pay_password, orderRemarks)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.showPay(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null) {
                            if (e instanceof ApiException) {
                                mView.showPayError(((ApiException) e).getCode(), e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                })
        );
    }

    @Override
    public void attachView(ResaleSureOrderContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == currentType;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        AddressListResponBean addressListResponBean = (AddressListResponBean) s.getData();
                        if (mView != null) {
                            mView.showSelectedAddress(addressListResponBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    /**
     * 判断是否设置支付密码
     */
    @Override
    public void getIsbindPwd() {
        //刷新获取数据
        UserInfoResponBean userInfoResponBean = mDataManager.getUserInfoResponBean();
        if (userInfoResponBean != null && userInfoResponBean.getIs_password() == 1) {
            if (mView != null) {
                mView.isbindPwd();
            }
        } else {
            if (mView != null) {
                mView.nobindPwd();
            }
        }
    }

}
